Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font Library: ensure merged fontFace data is enconded as an array instead of an object #54435

Merged
merged 2 commits into from
Sep 13, 2023

Conversation

matiasbenedetto
Copy link
Contributor

@matiasbenedetto matiasbenedetto commented Sep 13, 2023

What?

  • Ensure merged fontFace data is JSON encoded as an array instead of an object.
  • Add a test case to ensure that.

Why?

To fix a bug consisting of fontFace data being JSON encoded as an object ( {...} ) instead of an array ( [...] ).

When we remove duplicates, we could end up with missing sequential array keys in the fontFace list.

Because of the nature of PHP arrays and how they are encoded into JSON. In PHP, arrays can behave like indexed arrays or associative arrays (hashmaps). When they are encoded into JSON, indexed arrays become JSON arrays (e.g., [...]), while associative arrays become JSON objects (e.g., {...}).

The reason fontFace is being encoded as an object in your JSON output is due to the non-sequential keys.

How?

To ensure that fontFace is always encoded as a JSON array, I reset the keys of the $unique_faces array using array_values() before assigning it to $merged_font['fontFace']. This will reindex the keys and make sure they are sequential.

Testing Instructions

Run PHP unit tests with and without calling array_values() on this line:

$merged_font['fontFace'] = array_values( $unique_faces );
$merged_font['fontFace'] =  $unique_faces;

The added test case should fail when array_values() is not used.

@matiasbenedetto matiasbenedetto added [Type] Bug An existing feature does not function as intended Needs PHP backport Needs PHP backport to Core [Feature] Typography Font and typography-related issues and PRs labels Sep 13, 2023
@github-actions
Copy link

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/experimental/fonts/font-library/class-wp-font-family-utils.php
❔ phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php

@github-actions
Copy link

github-actions bot commented Sep 13, 2023

Flaky tests detected in 6d894c4.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6175255285
📝 Reported issues:

Copy link
Contributor

@jffng jffng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, I ran into a similar bug working on the duplicate variants issue, which was fixed by using array_values. Thanks!

@matiasbenedetto matiasbenedetto enabled auto-merge (squash) September 13, 2023 21:50
@matiasbenedetto matiasbenedetto merged commit fec5ffc into trunk Sep 13, 2023
@matiasbenedetto matiasbenedetto deleted the fix/ensure-fontface-encoded-as-array branch September 13, 2023 22:19
@github-actions github-actions bot added this to the Gutenberg 16.7 milestone Sep 13, 2023
@youknowriad youknowriad added Backported to WP Core Pull request that has been successfully merged into WP Core and removed Needs PHP backport Needs PHP backport to Core labels Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported to WP Core Pull request that has been successfully merged into WP Core [Feature] Typography Font and typography-related issues and PRs [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants